#include "McsFrame.h"
#include "util/Util.h"


McsFrame::~McsFrame()
{
    for(MultChannel* mcp : multChannels)
        delete mcp;
}

bool McsFrame::TryLoad(std::istream &i)
{
    for(MultChannel* mcp : multChannels)
        delete mcp;
    multChannels.clear();
    MultChannel* mcp;
    mcp = new MultChannel();
    mcp -> TryLoad(i);
    multChannels.emplace_back(mcp);
    if(mcshp->IsWithShadow())
    {
        mcp = new MultChannel();
        mcp -> TryLoad(i);
        multChannels.emplace_back(mcp);
    }
    if(mcshp->IsWithRemap())
    {
        mcp = new MultChannel();
        mcp -> TryLoad(i);
        multChannels.emplace_back(mcp);
    }
    return true;
}

bool McsFrame::TryDump(std::ostream &o)
{
    if(multChannels.empty())
    {
        throw StrException("多通道数据量错误");
    }
    for(const auto& i : multChannels)
    {
        i -> TryDump(o);
    }
    return true;
}

bool McsFrame::IsDumpable()
{
    for(MultChannel* mcp : multChannels)
        if(!(mcp -> IsDumpable()))
            return false;
    if(!(multChannels.size()))
        return false;
    if(multChannels[0] -> GetMultChannelHead().GetChannelCount() != 4)
        return false;
    if(mcshp->IsWithShadow())
        if(multChannels[1] -> GetMultChannelHead().GetChannelCount() != 1)
            return false;
    if(mcshp->IsWithRemap())
        if(multChannels[mcshp->IsWithShadow()?2:1] -> GetMultChannelHead().GetChannelCount() != 1)
            return false;
    return true;
}
